import { computed, ref } from "vue"
import { defineStore, acceptHMRUpdate } from "pinia"

export const tabStore = defineStore('tabs', ()=>{
    const data = ref({
        activeTabName: '',
        tabList: [] as TabInfo[]
    })

    function $reset(){
        return new Promise(resovle=>{
            data.value = {
                activeTabName: '',
                tabList:[]
            }
    
            localStorage.setItem('tabs', JSON.stringify(data.value))
            resovle(0)
        })
    }

    const tabList = computed(()=>{
        getStorage()
        return data.value.tabList
    })

    const toTab = computed(():any=>{
        getStorage()
        for(const tab of data.value.tabList){
            if(tab.name == data.value.activeTabName){
                return tab
            }
        }
    })

    const activeTabName = computed(() => {
        getStorage()
        return data.value.activeTabName
    })

    function setActiveTabName(activeTabName : string){
        return new Promise(resolve =>{
            getStorage()
            data.value.activeTabName = activeTabName

            localStorage.setItem('tabs', JSON.stringify(data.value))
            resolve(0)
        })
    }

    function closeOthers(title: string){
        return new Promise<string>(resolve  => {
            getStorage()
            let tabs = [] as TabInfo[]
            for(const tab of data.value.tabList){
                if(tab.title == title){
                    tabs.push(tab)
                }
            }
            if(tabs.length > 0){
                data.value = {
                    activeTabName: tabs[tabs.length - 1].name,
                    tabList: tabs
                }
            }else{
                data.value = {
                    activeTabName: '',
                    tabList: tabs
                }
            }
            
            localStorage.setItem('tabs', JSON.stringify(data.value))
            resolve(data.value.activeTabName)
        })
    }

    function addTab(title: string, name: string, params: object, query: object, hash: string, path: string){
        return new Promise(resolve  => {
            getStorage()
            let flag = false
            for(const tab of data.value.tabList){
                if(tab.name == name){
                    tab.params = params
                    tab.query = query
                    tab.hash = hash
                    tab.path = path
                    flag = true
                    break
                }
            }
            data.value.activeTabName = name
            if(!flag){
                data.value.tabList.push({
                    title,   name, params, query, hash, path
                })
            } 

            localStorage.setItem('tabs', JSON.stringify(data.value))
            resolve(0) 
        })
          
    }

    function removeTab( name:string ){
        localStorage.removeItem(name)
        return new Promise(resolve =>{
            getStorage()
            let tabs = [] as TabInfo[]
            for(const tab of data.value.tabList){
                if(tab.name != name){
                    tabs.push(tab)
                }
            }
            if(tabs.length > 0){
                data.value = {
                    activeTabName: tabs[tabs.length - 1].name,
                    tabList: tabs
                }
            }else{
                data.value = {
                    activeTabName: '',
                    tabList: tabs
                }
            }
            
            localStorage.setItem('tabs', JSON.stringify(data.value))
            resolve(0)
        })
        
    }

    function getStorage(){
        const storageData = localStorage.getItem('tabs')
        if(null != storageData){
            data.value = JSON.parse(storageData) 
        }
    }
    
    return { activeTabName, tabList, $reset, addTab, removeTab, setActiveTabName, closeOthers, toTab }
})

if(import.meta.hot){
    import.meta.hot.accept(acceptHMRUpdate(tabStore, import.meta.hot))
}

interface TabInfo{
    name: string,
    title: string,
    params: object,
    query: object,
    hash: string,
    path: string
}

